<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\cache\driver\Redis;
class Salemake extends Auth
{

	public function __construct(Request $request){
        parent::__construct($request);
        $this->data = $request->param();
        $this->lang = isset($this->data['lang']) && $this->data['lang'] ? $this->data['lang'] : is_lang();
        $this->user_id = session('userid') == null ? 1 : session('userid');
        $this->saleModel = model('SaleModel');
        $this->AdminWarehouse_id = $this->saleModel->getAdminWarehouse($this->user_id);
    }


    // =仓管员到中转区
    // =配货主管提货到散货区
    // =包装主管提货到包装区
    // =装箱单好了到待发区

    function getChecker()
    {
        $data = getDatas('position',['id'],['pid'=>29]);
        $position_ids = array_column($data, 'id');
        $position_ids[] = 29;
        $position_idsStr = implode($position_ids,',');
        $checker = getDatas('admin',['id','nickname checker_name'],['position_id'=>['in',$position_idsStr]]);
        if(!empty($checker)){
            foreach ($checker as $k => $v) {
                $checker[$k]['checker_num'] = hasData('sale_make',['checker_id'=>$v['id']]);
            }
        }
        // dump($checker);die;
        return $checker;
    }

    // 
    public function list(Request $request){

        if(isset($this->data['request']) && $this->data['request'] == 'requestAjax'){
            $page = "1,1000000";
            $returnData['pageSum'] = 0;
            $returnData['page'] = 1;
            $pageSize = 1000000;
            if(isset($this->data['page']) && isset($this->data['pageSize'])){
                $page = $this->data['page'].','.$this->data['pageSize'];
                $returnData['page'] = $this->data['page'];
                $pageSize = $this->data['pageSize'];
            }
            $returnData['checker'] = $this->getChecker(); 

            if(isset($this->data['requestType']) && $this->data['requestType'] == 'newList'){
                if(isset($this->data['myWork']) && $this->data['myWork'] == 1 ) $where['create_id'] = $this->user_id;

                $where['lock_at'] = 0;
                $where['del_at'] = 0;
                $where['underwrite'] = 0;
                $where['warehouse_id'] = $this->AdminWarehouse_id;

                $list = $this->getNewList($page,$where,'newList')['list'];
                $returnData['list'] = [];
                if(!empty($list)){
                    foreach ($list as $k => $v) {
                        if(!hasData('sale_make',['sale_id'=>$v['id']])){
                            if($v['makefirst'] == 1){
                                array_unshift($returnData['list'],$v);
                            }else{
                                if($v['pledge'] == 0){
                                    $returnData['list'][] = $v;
                                    $returnData['pageSum'] += count($v['detail']);
                                }else{
                                    if($v['paid']/$v['pay'] > 0.2){
                                        $returnData['list'][] = $v;
                                        $returnData['pageSum'] += count($v['detail']);
                                    }
                                }    
                            }
                        }
                    }
                    $returnData['list'] = $this->saleModel->getPagination($returnData,$pageSize,$returnData['page']);
                }
                

            }else if( isset($this->data['requestType']) && ($this->data['requestType'] == 'making' || $this->data['requestType'] == 'pause' || $this->data['requestType'] == 'delivery' || $this->data['requestType'] == 'finish' || $this->data['requestType'] == 'cancel') ){
                $returnData = $this->making($returnData['page'],$pageSize);

            }else if(isset($this->data['requestType']) && $this->data['requestType'] == 'missing'){
                $returnData = $this->getMissing($page);
            }

            $returnData['page'] = 1;
            if(isset($this->data['page']) && isset($this->data['pageSize'])){
                $returnData['page'] = $this->data['page'];
            }
            // dump($returnData);die;
            return json_encode(array('status'=>1,'data'=>$returnData));
        }else{
            return view('list',['isshow'=>1]);
        }
    }

    // 获取新订单数据
    function getNewList($page,$where,$type)
    {   
        $list = getDatas('sale',['*'],$where);  
        foreach ($list as $k => $v) {
            $list[$k]['company_name'] = $v['co_id'] ? getValue('company',['id'=>$v['co_id']],'name') : '';
            $list[$k]['branchcompany_name'] = $v['co_branch_id'] ? getValue('co_branch',['id'=>$v['co_branch_id']],'name') : '';
            $list[$k]['pledge'] = $v['pledge'];
            $list[$k]['paid'] = $v['paid'];
            $list[$k]['pay'] = $v['pay'];

            $list[$k]['saler_name'] = getValue('admin',['id'=>$v['saler_id']],'nickname');
            $list[$k]['create_name'] = getValue('admin',['id'=>$v['create_id']],'nickname');
            $list[$k]['create_time'] = getTimeDates($v['create_at'],$this->lang);
            $list[$k]['checker_id'] = getValue('sale_make',['sale_id'=>$v['id']],'checker_id');
            $list[$k]['checker_name'] = getValue('admin',['id'=>$list[$k]['checker_id']],'nickname');
            $list[$k]['delivery_type_name'] = getValue('multi_lang',['mkey'=>'sale_delivery_type_'.$list[$k]['delivery_type_id']],'content');
            // $list[$k]['detail']['print_num'] = 1;
            // $list[$k]['detail']['print_at'] = getTimeDates(time(),$this->lang);;

            $list[$k]['detail'] = getDatas('sale_detail',['*'],['sale_id'=>$v['id']]);
            if(!empty($list[$k]['detail'])) {
                foreach ($list[$k]['detail'] as $k1 => $v1) {
                    $list[$k]['detail'][$k1]['proInfo'] = getProAndProchar($v1,$this->lang,48);
                    // dump($v1['id']);die;
                    $list[$k]['detail'][$k1]['remark'] = getDatas('sale_remark',['*'],['sale_detail_id'=>$v1['id']]);
                    $list[$k]['detail'][$k1]['allocation'] = intval($v1['qty']/$list[$k]['detail'][$k1]['proInfo']['prochar_qty']);
                    $list[$k]['detail'][$k1]['allocation_'] = $v1['qty']-$list[$k]['detail'][$k1]['proInfo']['prochar_qty']*$list[$k]['detail'][$k1]['allocation'];
                    // dump($list[$k]['detail'][$k1]['proInfo']['prochar_qty']);die;
                    $list[$k]['detail'][$k1]['pickup'] = $this->saleModel->getPickup($v['id'],$v1['pro_id'],$v1['prochar_id'],$list[$k]['detail'][$k1]['proInfo']['prochar_qty']);
                    $list[$k]['detail'][$k1]['process'] = $this->saleModel->getProcess($v['id'],$v1['pro_id'],$v1['prochar_id'],$list[$k]['detail'][$k1]['proInfo']['prochar_qty']);
                    $list[$k]['detail'][$k1]['storeOrder'] = $this->saleModel->getStoreOrder($v['id'],$v1['pro_id'],$v1['prochar_id'],$list[$k]['detail'][$k1]['proInfo']['prochar_qty']);

                    $list[$k]['detail'][$k1]['storeInfo'] = $this->saleModel->getdetailStoreInfo($v1['pro_id'],$v1['prochar_id'],$v['warehouse_id'],$list[$k]['detail'][$k1]['proInfo']['prochar_qty'],$this->lang);
                    if($type == 'making' || $type == 'pause' || $type == 'cancel' ){
                        $where_ = ['sale_id'=>$v1['sale_id'],'pro_id'=>$v1['pro_id'],'prochar_id'=>$v1['prochar_id']];
                        // 仓管员
                        $list[$k]['detail'][$k1]['docker_name'] = $this->saleModel->getDockerAOrPacker('make_pickup_detail',$where_,'pickup_id','make_pickup');
                        // 包装主管（加工）   
                        $list[$k]['detail'][$k1]['packer_name'] = $this->saleModel->getDockerAOrPacker('make_process_detail',$where_,'process_id','make_process');

                        $store_missing = Db::name('store_missing')->field(['missing_qty,create_at,create_id'])->where(['check_id'=>0,'pro_id'=>$v1['pro_id'],'prochar_id'=>$v1['prochar_id'],'warehouse_id'=>$v['warehouse_id']])->find();

                        $list[$k]['detail'][$k1]['missing_qty'] = $store_missing['missing_qty'];

                        $missing_create_nanme = getValue('admin',['id'=>$store_missing['create_id'],'position_id'=>28],'nickname');

                        $list[$k]['detail'][$k1]['missing_create_name'] = $missing_create_nanme ? $missing_create_nanme : '-';
                        $list[$k]['detail'][$k1]['missing_create_time'] = $missing_create_nanme ? getTimeDates($store_missing['create_at'],$this->lang) : '-';
                    }
                }
            }
        }
        // $returnData['checker'] = $this->getChecker();
        $returnData['list'] = $list;
        return $returnData;
    }

    // 获取进行中/暂停/送货中的数据
    function making($page,$pageSize)
    {   
        if(isset($this->data['myWork']) && $this->data['myWork'] == 1 ) $where['create_id'] = $this->user_id;
        $where = [];
        $where['warehouse_id'] = $this->AdminWarehouse_id;
        if($this->data['requestType'] == 'making'){
            $where['maked_at'] = 0;
            $where['pause_at'] = 0;
        }

        if($this->data['requestType'] == 'pause') $where['pause_at'] = ['gt',0];
        if($this->data['requestType'] == 'delivery'){
            $where['delivery_at'] = ['gt',0];
            $where['receive_at'] = 0;
        } 
        if($this->data['requestType'] == 'finish') $where['receive_at'] = ['gt',0];

        if($this->data['requestType'] == 'cancel') $where['cancel_at'] = ['gt',0];

        if($this->data['is_me'] == 'true') $where['create_id'] = $this->user_id;

        $returnData['list'] = [];
        $returnData['checker'] = [];
        $res = getDatas('sale_make',['*'],$where,'id desc');
        $returnDataa['pageSum'] = 0;
        if(!empty($res)){
            foreach ($res as $k => $v) {
                $result =  $this->getNewList('1,1000000',['id'=>$v['sale_id'],'warehouse_id'=>$this->AdminWarehouse_id],$this->data['requestType']);
                if(!empty($result)){
                    $list = $result['list'];
                
                    foreach ($list as $key => $value) {
                        $list[$key]['print_count'] = $v['print_count'] == 0 ? '-' :$v['print_count'];
                        $list[$key]['print_at'] = $v['print_at'];
                        $list[$key]['print_time'] = $v['print_at'] ? getTimeDates($v['print_at'],$this->lang) : '-';

                        $list[$key]['pause_name'] = getValue('admin',['id'=>$v['pause_id']],'nickname');
                        $list[$key]['pause_at'] = $v['pause_at'];
                        $list[$key]['pause_time'] = $v['pause_at'] ? getTimeDates($v['pause_at'],$this->lang) : '-';

                        $list[$key]['checker_name'] = getValue('admin',['id'=>$v['checker_id']],'nickname');

                        $list[$key]['make_id'] = $v['id'];
                        $list[$key]['create_make_name'] = getValue('admin',['id'=>$v['create_id']],'nickname');
                        $list[$key]['create_make_time'] = getTimeDates($v['create_at'],$this->lang);
                        $list[$key]['create_make_at'] = $v['create_at'];

                        // 配货时间
                        $list[$key]['making_time'] = $v['maked_at'] ? date("H:i:s",$v['maked_at'] - $v['create_at']) : date("H:i:s",time() - $v['create_at']);

                        $sale_pack = Db::name('sale_pack')->field(['create_id,create_at'])->where(['sale_id'=>$v['sale_id']])->find();
                        if(!empty($sale_pack)){
                            $list[$key]['pack_name'] = getValue('admin',['id'=>$sale_pack['create_id']],'nickname');
                            $list[$key]['pack_time'] = getTimeDates($sale_pack['create_at'],$this->lang);
                        }else{
                            $list[$key]['pack_name'] = '-';
                            $list[$key]['pack_time'] = '-';
                        }
                        
                        if($this->data['requestType'] == 'delivery' || $this->data['requestType'] == 'finish'){
                            $list[$key]['make_delivery'] = $this->getSaleTeamIds($v['sale_id']);
                            $salePack = Db::name('sale_pack')->where(['sale_id'=>$v['sale_id']])->find();
                            $list[$key]['salePack_name'] = getValue('admin',['id'=>$salePack['create_id']],'nickname');
                            $list[$key]['salePack_time'] = getTimeDates($salePack['create_at'],$this->lang);
                        }
                    }
                    $returnDataa['pageSum'] += count($list[0]['detail']);
                    $returnData['list'][] = $list[0];
                }
            }
        }
        $returnDataa['list'] = $this->saleModel->getPagination($returnData,$pageSize,$page);
        return $returnDataa;
    }


    function getMissing($page)
    {
        $list = Db::name('store_missing')->where(['del_at'=>0,'warehouse_id'=>$this->AdminWarehouse_id])->order('id desc')->page($page)->select();
        if(!empty($list)){
            foreach ($list as $k => $v) {
                $list[$k]['proInfo'] = getProAndProchar($v,$this->lang,48);
                $list[$k]['storeInfo'] = $this->saleModel->getdetailStoreInfo($v['pro_id'],$v['prochar_id'],$v['warehouse_id'],$list[$k]['proInfo']['prochar_qty'],$this->lang);

                $sale_ids = implode( array_unique(array_column( getDatas('sale',['id'],['create_at'=>['gt',$v['create_at']]]),'id') ),' / ');
                $list[$k]['sale_id'] = getValue('store_missing_detail',['missing_id'=>$v['id']],'sale_id');
                $list[$k]['sale_ids'] = $sale_ids ? numzero($list[$k]['sale_id']).' / '. numzero($sale_ids) : numzero($list[$k]['sale_id']);
                $list[$k]['create_name'] = getValue('admin',['id'=>$v['create_id']],'nickname');
                $list[$k]['create_time'] = getTimeDates($v['create_at'],$this->lang);

                $list[$k]['update_name'] = getValue('admin',['id'=>$v['update_id']],'nickname');
                $list[$k]['update_time'] = getTimeDates($v['update_at'],$this->lang);

                $list[$k]['sale_detail_qty'] = getValue('sale_detail',['sale_id'=>$list[$k]['sale_id'],'pro_id'=>$v['pro_id'],'prochar_id'=>$v['prochar_id']],'qty');
                $list[$k]['allocation'] = intval($list[$k]['sale_detail_qty']/$list[$k]['proInfo']['prochar_qty']);
                $list[$k]['allocation_'] = $list[$k]['sale_detail_qty']-$list[$k]['proInfo']['prochar_qty']*$list[$k]['allocation'];
                $where = ['sale_id'=>$list[$k]['sale_id'],'pro_id'=>$v['pro_id'],'prochar_id'=>$v['prochar_id']];
                // 仓管员;
                $missing_create_nanme = getValue('admin',['id'=>$v['create_id'],'position_id'=>28],'nickname');
                $list[$k]['docker_name'] = $missing_create_nanme ? getValue('admin',['id'=>$v['create_id']],'nickname') : '-';
                $list[$k]['docker_time'] = $missing_create_nanme ? getTimeDates($v['create_at'],$this->lang) : '-';
                
                // 加工说明
                $list[$k]['processRemark'] = Db::name('make_process_remark')
                        ->where('detail_id', 'IN', function ($query) use ($where) {
                            $query->name('make_process_detail')->where($where)->field('id');
                        })
                        ->select();

            }
        }
        $returnData['list'] = $list;
        $returnData['pageSum'] = hasData('store_missing',['del_at'=>0]);
        return $returnData;
    }

    // 获取销售单指定的送货员工
    function getSaleTeamIds($sale_id)
    {
        $delivery_id = getValue('make_delivery_detail',['sale_id'=>$sale_id],'delivery_id');
        $create_at = getValue('make_delivery',['id'=>$delivery_id],'create_at');
        $team_ids = getDatas('make_delivery_team',['admin_id'],['delivery_id'=>$delivery_id]);
        $team_ids = implode(array_column($team_ids, 'admin_id'),',');
        if($team_ids){
            $returnData['teams'] = getDatas('admin',['id,nickname'],['id'=>['in',$team_ids]]);
            $returnData['create_time'] = getTimeDates($create_at,$this->lang);
            return $returnData;
        }else{
            return [];
        }
    }


    //  新订单 开始配单
    function startDistribute()
    {
        // 生成配货开始
        // 提货写到 make_pickup_detail
        // 加工写到 make_process_detail
        // 调货写到 store_order_detail
        // 注意： 提交的数据是件数，因此mysal——qty 要乘每件数量
        if(isset($this->data['id'])){
            if($this->data['requestType'] == 'get'){
                $where['area_id'] = 2;
                $where['pro_id'] = $this->data['pro_id'];
                $where['prochar_id'] = $this->data['prochar_id'];
                $where['warehouse_id'] = $this->data['warehouse_id'];
                $store2areaQty = DB::name('store2area')->where($where)->sum('qty');
                if($this->data['allocation_'] > $store2areaQty){
                    return 1;
                }else{
                    return 0;
                }
            }else{
                // 生成 销售订单配货执行记录 
                $sale_make_id = getValue('sale_make',['sale_id'=>$this->data['id']],'id');
                if($sale_make_id){
                    $sale_make = updateData('sale_make',['id'=>$sale_make_id],['update_at'=>time(),'update_id'=>$this->user_id,'checker_id'=>$this->data['checker_id'],'warehouse_id'=>$this->data['warehouse_id']]);
                }else{
                    $sale_make_id = insertData('sale_make',['sale_id'=>$this->data['id'],'create_at'=>time(),'create_id'=>$this->user_id,'update_at'=>time(),'update_id'=>$this->user_id,'checker_id'=>$this->data['checker_id'],'pickup_at'=>0,'process_at'=>0,'warehouse_id'=>$this->data['warehouse_id']]);
                }
                $pickup_at = false;
                $process_at = false;
                foreach ($this->data['detail'] as $k => $v) {
                    $sqlData['pro_id'] = $v['pro_id'];
                    $sqlData['prochar_id'] = $v['prochar_id'];
                    $sqlData['make_id'] = $sale_make_id;
                    $sqlData['sale_id'] = $v['sale_id'];    
                    $sqlData['create_id'] = $this->user_id;                
                    $sqlData['create_at'] = time();                
                    $sqlData['priority'] = $this->data['priority'];

                    if(isset($v['pickup']) && $v['pickup'] ){   // 提货
                        $aa = $this->makeRankCalculation('make_pickup_detail');
                        $sqlData['qty'] = $v['pickup']*$v['proInfo']['prochar_qty'];
                        $make_pickup_detail_id = getValue('make_pickup_detail',['sale_id'=>$this->data['id'],'pro_id'=>$v['pro_id'],'prochar_id'=>$v['prochar_id']],'id');
                        if($make_pickup_detail_id){
                            $res1 = updateData('make_pickup_detail',['id'=>$make_pickup_detail_id],['qty'=>$sqlData['qty']]);
                        }else{
                            $sqlData['rank'] = 1;
                            $sqlData['warehouse_id'] = $this->data['warehouse_id'];   
                            $res1 = insertData('make_pickup_detail',$sqlData);
                        }
                        $pickup_at = true;
                    }
                    if(isset($v['process']) && $v['process'] ){ // 加工
                        $aa = $this->makeRankCalculation('make_process_detail');
                        $sqlData['qty'] = $v['process']*$v['proInfo']['prochar_qty'];
                        $make_process_detail_id = getValue('make_process_detail',['sale_id'=>$this->data['id'],'pro_id'=>$v['pro_id'],'prochar_id'=>$v['prochar_id']],'id');
                        if($make_process_detail_id){
                            $res1 = updateData('make_process_detail',['id'=>$make_process_detail_id],['qty'=>$sqlData['qty']]);
                        }else{
                            $sqlData['rank'] = 1;
                            $sqlData['warehouse_id'] = $this->data['warehouse_id'];   
                            $res1 = insertData('make_process_detail',$sqlData);
                        }
                        $process_at = true;
                    }

                    if(isset($v['storeOrder']) && $v['storeOrder'] ){ // 调货
                        // 判断当前所在仓库是有哦足够量的产品，以判断要不要生成调货数据。
                        $store_order_detail_qty = $v['storeOrder']*$v['proInfo']['prochar_qty'];
                        if($store_order_detail_qty > 0 ){
                            $store_order_detail_id = getValue('store_order_detail',['sale_id'=>$this->data['id'],'pro_id'=>$v['pro_id'],'prochar_id'=>$v['prochar_id']],'id');
                            if(!$store_order_detail_id){
                                $res1 = insertData('store_order_detail',['store_order_id'=>0,'pro_id'=>$v['pro_id'],'prochar_id'=>$v['prochar_id'],'qty'=>$store_order_detail_qty,'sale_id'=>$v['sale_id'],'warehouse_id'=>$this->data['warehouse_id'],'create_id'=>$this->user_id,'create_at'=>time(),'update_id'=>$this->user_id,'update_at'=>time()]);
                            }else{
                                $res1 = updateData('store_order_detail',['id'=>$store_order_detail_id],['qty'=>$store_order_detail_qty]);
                            }    
                        }
                    }
                }

                if($pickup_at) updateData('sale_make',['id'=>$sale_make_id],['pickup_at'=>0]);
                if($process_at) updateData('sale_make',['id'=>$sale_make_id],['process_at'=>0]);

                if($sale_make_id >= 0){
                    updateData('sale',['id'=>$this->data['id']],['is_new'=>1,'make_id'=>$sale_make_id]);
                    $this->saleModel->addSaleLog($this->data['id'],'makeMake','配货主管开始配单');
                    return json_encode(array('status'=>1,'msg'=>'配单成功'));
                }else{
                    return json_encode(array('status'=>2,'msg'=>'配单失败'));
                }
            }
        }else{
            return json_encode(array('status'=>2,'msg'=>'配单失败，提交正确的数据'));
        }


    }

    // 配货 新增提货 获取系统计算字段值
    function makeRankCalculation($table)
    {   
        $data = getDatas($table,['*'],['finish_at'=>0,'rank'=>['lt',9]]);
        if(!empty($data)){
            foreach ($data as $k => $v) {
                $start_at = ceil((time() - $v['start_at'])/(30*60));
                $rank = $start_at > 9 ? 9 : $start_at;
                $res = updateData($table,['id'=>$v['id']],['rank'=>$rank]);
            }
        }
        return true;
    }

    // 进行中 配货保存数据
    function saveProcessingData()
    {   
        // dump($this->data);die;
        if(!empty($this->data['data']) && $this->data['request'] == 'save'){
            $where = [
                        'sale_id'=>$this->data['data']['sale_id'],
                        'pro_id'=>$this->data['data']['pro_id'],
                        'prochar_id'=>$this->data['data']['prochar_id']
                    ];
            if(isset($this->data['packup']) && $this->data['packup'] > 0){
                if(hasData('make_pickup_detail',$where)){
                    $res = updateData('make_pickup_detail',$where,['qty'=>$this->data['packup']*$this->data['data']['proInfo']['prochar_qty']]); 
                }else{
                    $res = insertData('make_pickup_detail',['sale_id'=>$this->data['data']['sale_id'],'pro_id'=>$this->data['data']['pro_id'],'prochar_id'=>$this->data['data']['prochar_id'],'qty'=>$this->data['packup']*$this->data['data']['proInfo']['prochar_qty'],'rank'=>1,'create_id'=>$this->user_id,'create_at'=>time()]); 
                }
            }else{
                $res = deleteData('make_pickup_detail',$where); 
            }
                
            if(isset($this->data['process']) && $this->data['process'] > 0){
                if(hasData('make_process_detail',$where)){
                    $res = updateData('make_process_detail',$where,['qty'=>$this->data['process']]);
                }else{
                    $res = insertData('make_process_detail',['sale_id'=>$this->data['data']['sale_id'],'pro_id'=>$this->data['data']['pro_id'],'prochar_id'=>$this->data['data']['prochar_id'],'qty'=>$this->data['process'],'rank'=>1,'create_id'=>$this->user_id,'create_at'=>time()]); 
                }
            }else{
                $res = deleteData('make_process_detail',$where); 
            }

            if($res){
                return json_encode(array('status'=>1,'msg'=>'调配成功'));
            }else{
                return json_encode(array('status'=>0,'msg'=>'调配失败'));
            }
        }else if($this->data['request'] == 'is_startAt'){
            $where = [
                        'sale_id'=>$this->data['sale_id'],
                        'pro_id'=>$this->data['pro_id'],
                        'prochar_id'=>$this->data['prochar_id']
                    ];
            $return['pickup_detail'] = 0;
            $return['process_detail'] = 0;
            // 可以被编辑的条件
            $pickup_detail = Db::name('make_pickup_detail')->field(['start_at,pause_at'])->where($where)->find();
            if(empty($pickup_detail)) $return['process_detail'] = 1;
            if(!empty($pickup_detail) && $pickup_detail['start_at'] == 0) $return['pickup_detail'] = 1;
            if(!empty($pickup_detail) && $pickup_detail['start_at'] > 0 && $pickup_detail['pause_at'] > 0) $return['pickup_detail'] = 1;
            
            $process_detail = Db::name('make_process_detail')->field(['start_at,pause_at'])->where($where)->find();
            if(empty($process_detail)) $return['process_detail'] = 1;
            if(!empty($process_detail) && $process_detail['start_at'] == 0) $return['process_detail'] = 1;
            if(!empty($process_detail) && $process_detail['start_at'] > 0 && $process_detail['pause_at'] > 0) $return['process_detail'] = 1;
            return json_encode(array('status'=>1,'data'=>$return));
        }else{
            return json_encode(array('status'=>2,'msg'=>'调配失败，调配数据不对'));
        }
    }

    // 进行中 提货保存
    function saveProcessingPickup()
    {
        if(isset($this->data['requestType']) && $this->data['requestType'] == 'warehouseArea'){
            return json_encode(model('SaleModel')->getWarehouseArea(true,['type_id'=>['in','1,2'],'warehouse_id'=>$this->AdminWarehouse_id]));
        }else if(isset($this->data['requestType']) && $this->data['requestType'] == 'save'){
            // dump($this->data['data']);die;
            if(!empty($this->data['data'])){
                return $this->saveMakeDetail($this->data['data'],'make_pickup_detail');
            }else{
                return json_encode(array('status'=>2,'msg'=>'提交空数据'));
            }
        }else if(isset($this->data['requestType']) && $this->data['requestType'] == 'get'){
            // dump($this->data);die;
            return json_encode($this->getMakeDeatil($this->data['sale_id'],'make_pickup_detail'));
        }
    }

    // 进行中保存提货detail 数据
    function saveMakeDetail($data,$table)
    {
        // dump($data);
        $res = -1;
        foreach ($data as $k => $v) {
            if($table != 'store_order_detail'){
                $sqlData['priority'] = getValue('sale',['id'=>$v['sale_id']],'priority');
            }else{
                $sqlData['qty'] = $v['processingStoreOrderProQty'];
            }
            // 加工
            if($table == 'make_process_detail'){
                $sqlData['qty'] = $v['processingProcessProQty'];
            }
            // 提货
            if($table == 'make_pickup_detail'){
                $sqlData['qty'] = $v['processingPickupProQty'];
                $sqlData['area_id'] = $v['processingPickupArea_id'];
                $sqlData['target'] = 1;    

                $store2areaWhere['warehouse_id'] = getValue('sale',['id'=>$v['sale_id']],'warehouse_id');
                $store2areaWhere['pro_id'] = $v['proInfo']['pro_id'];
                $store2areaWhere['prochar_id'] = $v['proInfo']['prochar_id'];
                $store2areaWhere['area_id'] = $v['processingPickupArea_id'];

                $oldQty = getValue('make_pickup_detail',['sale_id'=>$v['sale_id'],'pro_id'=>$v['proInfo']['pro_id'],'prochar_id'=>$v['proInfo']['prochar_id']],'qty');

            }

            if(!isset($v['isset'])){
                // if($table != 'store_order_detail'){
                    $res = updateData($table,['sale_id'=>$v['sale_id'],'pro_id'=>$v['proInfo']['pro_id'],'prochar_id'=>$v['proInfo']['prochar_id']],$sqlData);
                // }else{
                //     $res = updateData($table,['sale_id'=>$v['sale_id'],'pro_id'=>$v['proInfo']['pro_id'],'prochar_id'=>$v['proInfo']['prochar_id']],$sqlData);
                // }
            }else if(isset($v['isset']) && $v['isset'] == 'false'){
                // 重新排序
                $sqlData['sale_id'] = $v['sale_id'];
                $sqlData['pro_id'] = $v['proInfo']['pro_id'];
                $sqlData['prochar_id'] = $v['proInfo']['prochar_id'];
                if($table != 'store_order_detail'){
                    $aa = $this->makeRankCalculation($table);
                    // $sqlData['start_at'] = time();
                    $sqlData['rank'] = 1;
                    $sqlData['warehouse_id'] = $this->AdminWarehouse_id;
                }
                $res = insertData($table,$sqlData);
                $oldQty = 0;
            }
            if($table == 'make_pickup_detail'){
                $m3 = $sqlData['qty']/$v['proInfo']['prochar_qty']*$v['proInfo']['ctn_m3'];
                $oldM3 = $oldQty/$v['proInfo']['prochar_qty']*$v['proInfo']['ctn_m3'];
                $this->store2areaSql($store2areaWhere,$oldQty,$oldM3,$sqlData['qty'],$m3);
            }

        }
        if($res >= 0){
            return json_encode(array('status'=>1,'msg'=>'通知成功'));
        }else{
            return json_encode(array('status'=>2,'msg'=>'通知失败'));
        }
    }

    function store2areaSql($where,$oldQty,$oldM3,$qty,$m3)
    {   
        // dump($m3);die;
        if(hasData('store2area',$where)){
            // 判断跟新的qty 和 旧qty 的大小
            if($oldQty > $qty){
                Db::name('store2area')->where($where)->setDec('qty',$oldQty-$qty);
                Db::name('store2area')->where($where)->setDec('volume',$oldM3-$m3);
            }else if($oldQty < $qty){
                Db::name('store2area')->where($where)->setInc('qty',$qty-$oldQty);
                Db::name('store2area')->where($where)->setInc('volume',$m3-$oldM3);
            }
        }else{
            $where['qty'] = $qty;
            $where['volume'] = $m3;
            insertData('store2area',$where);
        }
    }

    // 根据sale_id  获取make_pickup_detail  数据
    function getMakeDeatil($sale_id,$table)
    {
        $data = getDatas('sale_detail',['*'],['sale_id'=>$sale_id]);
        $list = [];
        if(!empty($data)) {
            $i=0;
            foreach ($data as $k1 => $v1) {
                // $detail = Db::name($table)->where(['sale_id'=>$sale_id,'pro_id'=>$v1['pro_id'],'prochar_id'=>$v1['prochar_id']])->find();
                $detail = Db::name($table)->where(['sale_id'=>$sale_id,'pro_id'=>$v1['pro_id'],'prochar_id'=>$v1['prochar_id']])->select();
                if(!empty($detail)){
                    foreach ($detail as $key => $value) {
                        $list[$i]['sale_id'] = $sale_id;
                        $list[$i]['qty'] = $v1['qty'];
                        $list[$i]['proInfo'] = getProAndProchar($v1,$this->lang,48);
                        // dump($v1['id']);die;
                        $list[$i]['remark'] = getDatas('sale_remark',['*'],['sale_detail_id'=>$v1['id']]);
                        $list[$i]['allocation'] = intval($v1['qty']/$list[$i]['proInfo']['prochar_qty']);
                        $list[$i]['allocation_'] = $v1['qty']-$list[$i]['proInfo']['prochar_qty']*$list[$i]['allocation'];
                        $list[$i]['storeInfo'] = $this->saleModel->getdetailStoreInfo($v1['pro_id'],$v1['prochar_id'],$this->AdminWarehouse_id,$list[$i]['proInfo']['prochar_qty'],$this->lang);
                        if($table == 'make_pickup_detail'){
                            // $list[$i]['pickup'] = $this->getPickup($sale_id,$v1['pro_id'],$v1['prochar_id']);
                            $list[$i]['processingPickupProQty'] = $value['qty'];
                            $list[$i]['processingPickupArea_id'] = $value['area_id'];
                            $list[$i]['processingPickupArea_name'] = getValue('warehouse_area',['id'=>$value['area_id']],'name');    
                        }
                        if($table == 'make_process_detail') $list[$i]['processingProcessProQty'] = $value['qty'];
                        if($table == 'store_order_detail') $list[$i]['processingStoreOrderProQty'] = $value['qty'];
                        $i++;
                    }
                }
            }
        }
        return $list;
    }


    // 进行中 加工保存
    function saveProcessingProcess()
    {
        if(isset($this->data['requestType']) && $this->data['requestType'] == 'save'){
            // dump($this->data['data']);die;
            if(!empty($this->data['data'])){
                return $this->saveMakeDetail($this->data['data'],'make_process_detail');
            }else{
                return json_encode(array('status'=>2,'msg'=>'提交空数据'));
            }
        }else if(isset($this->data['requestType']) && $this->data['requestType'] == 'get'){
            // dump($this->data);die;
            return json_encode($this->getMakeDeatil($this->data['sale_id'],'make_process_detail'));
        }
    }

    // 进行中 调货保存
    function saveProcessingStoreOrder()
    {
        if(isset($this->data['requestType']) && $this->data['requestType'] == 'save'){
            // dump($this->data['data']);die;
            if(!empty($this->data['data'])){

                return $this->saveMakeDetail($this->data['data'],'store_order_detail');
            }else{
                return json_encode(array('status'=>2,'msg'=>'提交空数据'));
            }
        }else if(isset($this->data['requestType']) && $this->data['requestType'] == 'get'){
            // dump($this->data);die;
            return json_encode($this->getMakeDeatil($this->data['sale_id'],'store_order_detail'));
        }
    }

    // 执行暂停动作
    function pauseAction($sale_id = 0,$type=true)
    {
        if($sale_id){

            if($type){
                $updateData = ['pause_at'=>time(),'pause_id'=>$this->user_id];
                $msg = '通知暂停';
            }else{
                $updateData = ['pause_at'=>0,'pause_id'=>0];
                $msg = '取消暂停';
            }

            $make_pause = updateData('sale_make',['sale_id'=>$sale_id],$updateData);
            if($make_pause){
                updateData('make_pickup_detail',['sale_id'=>$sale_id],$updateData);
                // 判断是否需要暂停make_pickup数据
                $pickup_res = getDatas('make_pickup_detail',['pickup_id'],['sale_id'=>$sale_id,'pickup_id'=>['gt',0]]);
                if(!empty($pickup_res)){
                    $pickup_ids = array_unique(array_column($pickup_res,'pickup_id'));
                    foreach ($pickup_ids as $k => $v) {
                       updateData('make_pickup',['id'=>$v],$updateData);
                    }
                }

                updateData('make_process_detail',['sale_id'=>$sale_id],$updateData);
                // 判断是否需要暂停make_process数据
                $process_res = getDatas('make_process_detail',['process_id'],['sale_id'=>$sale_id,'process_id'=>['gt',0]]);
                if(!empty($process_res)){
                    $process_ids = array_unique(array_column($process_res,'process_id'));
                    foreach ($process_ids as $k1 => $v1) {
                        updateData('make_process',['id'=>$v1],$updateData);
                    }
                }

                $this->saleModel->addSaleLog($sale_id,'makeMake','配货主管'.$msg.'配货');
                return json_encode(array('status'=>1,'msg'=>$msg.'成功'));
            }else{
                return json_encode(array('status'=>2,'msg'=>$msg.'失败'));
            }
        }else{
            return json_encode(array('status'=>2,'msg'=>$msg.'失败,选择正确的数据'));
        }
    }

    // 进行中暂停
    function processingPause()
    {
        if(isset($this->data['requestType']) && $this->data['requestType'] == 'save'){
            // dump($this->data);die;
             return $this->pauseAction($this->data['sale_id']);
        }else if( isset($this->data['requestType']) && $this->data['requestType'] == 'get'){
            return json_encode($this->getProcessingPaus($this->data['sale_id']));
        }
    }

    function getProcessingPaus($sale_id)
    {
        $list = $this->getNewList('',['id'=>$sale_id],'newList')['list'][0];
        // 获取销售单产品明细信息
        foreach ($list['detail'] as $k => $v) {
            // **************获取销售单明细 暂停信息 start***************
            $pauseInfo = getDatas('make_pickup_detail',['pause_at,pause_id'],['sale_id'=>$v['sale_id'],'pro_id'=>$v['pro_id'],'prochar_id'=>$v['prochar_id'],'pause_at'=>['gt',0]]);
            $list['detail'][$k]['pauseRemake'] = [];
            if(empty($pause)){
                $pauseInfo = getDatas('make_process_detail',['id,pause_at,pause_id'],['sale_id'=>$v['sale_id'],'pro_id'=>$v['pro_id'],'prochar_id'=>$v['prochar_id'],'pause_at'=>['gt',0]]);
                if(!empty($pauseInfo)){
                    $list['detail'][$k]['pauseRemake'] = getDatas('make_process_remark',['id,remark'],['detail_id'=>$pauseInfo[0]['id']]);
                }
                
            }else{
                $list['detail'][$k]['pauseRemake'][]['remark'] = '缺货';
            }

            if(!empty($pauseInfo)){
                $list['detail'][$k]['pause_name'] = getValue('admin',['id'=>$pauseInfo[0]['pause_id']],'nickname');
                $list['detail'][$k]['pause_time'] = getTimeDates($pauseInfo[0]['pause_at'],$this->lang);
            }else{
                $list['detail'][$k]['pause_name'] = '-';
                $list['detail'][$k]['pause_time'] = '-';
            }
            // **************获取销售单明细 暂停信息 end***************
        }

        // 包装主管
        $process_id = getValue('make_process_detail',['sale_id'=>$sale_id,'process_id'=>['gt',0]],'process_id');
        $list['process_name'] = $process_id ? getValue('admin',['id'=>getValue('make_process',['id'=>$process_id],'create_id')],'nickname') : '-';
        // 仓管员
        $pickup_id = getValue('make_pickup_detail',['sale_id'=>$sale_id,'pickup_id'=>['gt',0]],'pickup_id');
        $list['pickup_name'] = $pickup_id ? getValue('admin',['id'=>getValue('make_pickup',['id'=>$pickup_id],'create_id')],'nickname') : '-';
        
        // 销售单状态
        $list['status_name'] = '配货中';
        $sale_make = getDatas('sale_make',['*'],['sale_id'=>$sale_id])[0];
        if($sale_make['maked_at'] > 0) $list['status_name'] = '配货完成';
        if($sale_make['package_at'] > 0) $list['status_name'] = '装箱完成';
        if($sale_make['pause_at'] > 0) $list['status_name'] = '暂停中';
        if($sale_make['cancel_at'] > 0) $list['status_name'] = '取消中';

        // 获取销售单提货信息
        $pickupData = getDatas('make_pickup_detail',['*'],['sale_id'=>$sale_id]);
        if(!empty($pickupData)){
            foreach ($pickupData as $k1 => $v1) {
                $pickupData[$k1]['proInfo'] = getProAndProchar($v1,$this->lang,48);
                $pickupData[$k1]['area_name'] = $this->saleModel->getArea_name($v1['target'],$v1['area_id']);
                $pickupData[$k1]['create_name'] = getValue('admin',['id'=>$v1['create_id']],'nickname');
                $pickupData[$k1]['create_time'] = getTimeDates($v1['create_at'],$this->lang);
                $pickupData[$k1]['prickup_name'] = $v1['pickup_id'] ? getValue('admin',['id'=>getValue('make_process',['id'=>$v1['pickup_id']],'create_id')],'nickname') : '-';
                $pickupData[$k1]['status_name'] = '提货中';
                $time = 0;
                if($v1['pause_at']){
                    $pickupData[$k1]['status_name'] = '暂停中';
                    $time = $v1['pause_at'];
                }   
                    
                if($v1['cancel_at']){
                    $pickupData[$k1]['status_name'] = '已取消';
                    $time = $v1['cancel_at'];
                }
                if($v1['finish_at']){
                    $pickupData[$k1]['status_name'] = '提货完成';
                    $time = $v1['finish_at'];
                }
                $pickupData[$k1]['status_time'] = $time == 0 ? '-' :getTimeDates($time,$this->lang);

                $pickupData[$k1]['getback_name'] = $v1['getback_id'] ? getValue('admin',['id'=>$v1['getback_id']],'nickname') : '-' ;
                $pickupData[$k1]['getback_time'] = $v1['getback_at'] ? getTimeDates($v1['getback_at'],$this->lang) : '-';

                // 获取缺货数据
                $pickupData[$k1]['missing_qty'] = getValue('store_missing',['id'=>getValue('store_missing_detail',['sale_id'=>$sale_id,'sale_make_id'=>$sale_make['id']],'missing_id') ],'missing_qty');
            }
        }
        $list['pickup'] = $pickupData;

        // 获取销售单加工信息
        $processData = getDatas('make_process_detail',['*'],['sale_id'=>$sale_id]);
        // $processData = getSearch('make_process_detail','zu',['zu.*,p.team_ids'],['sale_id'=>$sale_id],[['make_process p','zu.process_id = p.id','LEFT']],'zu.id desc');
        if(!empty($processData)){
            foreach ($processData as $k2 => $v2) {
                $processData[$k2]['remark'] = getDatas('make_process_remark',['id,remark'],['detail_id'=>$v2['id']]);
                $processData[$k2]['create_name'] = getValue('admin',['id'=>$v2['create_id']],'nickname');
                $processData[$k2]['create_time'] = getTimeDates($v1['create_at'],$this->lang);
                $processData[$k2]['proInfo'] = getProAndProchar($v2,$this->lang,48);
            }
        }
        $list['process'] = $processData;




        return $list;
    }

                    
    // 暂停 -- 缺货登记
    function storeMissing()
    {   
        if(isset($this->data['request']) && $this->data['request'] == 'getStoreMissing'){
            return json_encode($this->getStoreMissing());
        }else if(isset($this->data['request']) && $this->data['request'] == 'save'){
            return $this->saveStoreMissing();
        }
    }

    // 获取确定登记数据
    function getStoreMissing()
    {
        return $this->saleModel->saleMissingList($this->data,$this->lang,$this->AdminWarehouse_id);
    }

    function saveStoreMissing()
    {
        // dump($this->data);die;
        $store_missing['pro_id'] = $this->data['missing']['pro_id'];
        $store_missing['warehouse_id'] = $this->AdminWarehouse_id;
        $store_missing['prochar_id'] = $this->data['missing']['prochar_id'];
        $store_missing['store_qty'] = $this->data['missing']['storeInfo']['store_sum'];
        $store_missing['missing_qty'] = $this->data['missing']['missing_qty'];
        $store_missing['update_id'] = $this->user_id;
        $store_missing['update_at'] = time();
        $store_missing['check_id'] = $this->user_id;
        $store_missing['read_at'] = time();

        $store_missing_id = getValue('store_missing',['warehouse_id'=>$this->AdminWarehouse_id,'pro_id'=>$this->data['missing']['pro_id'],'prochar_id'=>$this->data['missing']['prochar_id'],'read_at'=>0],'id');
        if($store_missing_id){
            $res1 = updateData('store_missing',['id'=>$store_missing_id],$store_missing);
        }else{
            $store_missing['create_id'] = $this->user_id;
            $store_missing['create_at'] = time();
            $store_missing_id = insertData('store_missing',$store_missing);
        }
        if(isset($this->data['detail'])){
            if($store_missing_id){
                $del = deleteData('store_missing_detail',['missing_id'=>$store_missing_id]);
                $store_missing_detail['missing_id'] = $store_missing_id;
                foreach ($this->data['detail'] as $k => $v) {
                    if(in_array($v['sale_id'], $this->data['sale_ids'])){
                        $store_missing_detail['sale_id'] = $v['sale_id'];
                        $sale_warehouse_id = getValue('sale',['id'=>$v['sale_id']],'warehouse_id');
                        $delivery_note_id = getValue('delivery_note_detail',['sale_id'=>$v['sale_id'],'pro_id'=>$this->data['missing']['pro_id'],'prochar_id'=>$this->data['missing']['prochar_id']],'delivery_note_id');
                        $store_missing_detail['sale_make_id'] = $this->getSaleMakeId($this->data['missing']['sale_id'],$sale_warehouse_id);
                        $store_missing_detail['sale_make_id'] = $delivery_note_id ? $delivery_note_id : 0;
                        $insert_store_missing_detail = insertData('store_missing_detail',$store_missing_detail);    
                    }
                    
                }
            }
        }
        return json_encode(array('status'=>1,'msg'=>'缺货数据登记成功'));
    }

    // 获取配配货的place.name /取消配货
    function cancelMake()
    {
        if(isset($this->data)){

            if(!empty($this->data['detail'])){
                if($this->data['requestType'] == 'ready'){
                    foreach ($this->data['detail'] as $k => $v) {
                        $this->data['detail'][$k]['backArea'] = getSearch('warehouse_area','zu',['zu.id,zu.name,a.qty'],['zu.warehouse_id'=>$this->data['warehouse_id'],'zu.type_id'=>2,'a.pro_id'=>$v['pro_id'],'a.prochar_id'=>$v['prochar_id']],[['store2area a','zu.id = a.area_id']],'a.qty asc');
                        if(empty($backArea)){
                            $this->data['detail'][$k]['backArea'] = getDatas('warehouse_area',['id,name'],['type_id'=>2]);
                        }
                        // 获取已提货的记录，然后原路退回
                        $make_id = getValue('sale_make',['sale_id'=>$this->data['id']],'id');
                        $pickupStore = getSearch('make_pickup_store','zu',['zu.*,w.name warehouse_name,wp.name place_name'],['zu.pro_id'=>$v['pro_id'],'zu.prochar_id'=>$v['prochar_id'],'zu.make_id'=>$make_id],[['warehouse w','zu.warehouse_id = w.id'],['warehouse_place wp','zu.place_id = wp.id']],'zu.id desc');
                        $this->data['detail'][$k]['pickupStore'] = [];
                        if(empty($pickupStore)){
                            unset($this->data['detail'][$k]);
                        }else{
                            $this->data['detail'][$k]['pickupStore'] = $pickupStore[0];
                        }  

                        // $pickupStore = getDatas('make_pickup_store',['*'],['pro_id'=>$v['pro_id'],'prochar_id'=>$v['prochar_id'],'make_id'=>$make_id]);
                    }    
                    unset($this->data['requestType']);
                    return json_encode($this->data);
                }else{
                    dump($this->data);die;
                    foreach ($this->data['detail'] as $k1 => $v1) {
                        
                        $storeWhere['pro_id'] = $v1['pickupStore']['pro_id'];
                        $storeWhere['prochar_id'] = $v1['pickupStore']['prochar_id'];
                        $storeWhere['place_id'] = $v1['pickupStore']['place_id'];
                        $storeWhere['warehouse_id'] = $v1['pickupStore']['warehouse_id'];
                        if($v1['allocation'] > 0) DB::name('store')->where($where)->setInc('qty',$v1['allocation']*$v1['proInfo']['prochar_qty']);
                        $area_id = 0;
                        foreach ($v1['backArea'] as $k2 => $v2) {
                            if(isset($v2['checked'])) $area_id = $v2['id'];
                        }
                        if($v['allocation_'] > 0 && $area_id > 0){
                            $store2areaWhere['area_id'] = $area_id;
                            $store2areaWhere['pro_id'] = $v1['pickupStore']['pro_id'];
                            $store2areaWhere['prochar_id'] = $v1['pickupStore']['prochar_id'];
                            $store2areaWhere['warehouse_id'] = $v1['pickupStore']['warehouse_id'];

                            // 判断选择的散货区  在store2area 是否有数据， 有就累加qty和volume，没有则新增数据
                            if(hasData('store2area',$store2areaWhere)){
                                DB::name('store2area')->where($store2areaWhere)->setInc('qty',$v1['allocation_']*$v1['proInfo']['prochar_qty']);
                                DB::name('store2area')->where($store2areaWhere)->setInc('volume',$v1['allocation_']*($v1['proInfo']['ctn_m3']/$v1['proInfo']['prochar_qty']));
                            }else{
                                $store2areaWhere['qty'] = $v1['allocation_']*$v1['proInfo']['prochar_qty'];
                                $store2areaWhere['volume'] = $v1['allocation_']*($v1['proInfo']['ctn_m3']/$v1['proInfo']['prochar_qty']);
                                insertData('store2area',$store2areaWhere);
                            }
                        } 
                        
                    }
                    return $this->sale_makeCancel($this->data['sale_id']);
                     // json_encode(array('status'=>1,'msg'=>'通知取消配货成功'));
                }
                
                // dump($this->data);die;
            }else{

                    dump('empty');die;
            }
        }
    }

    //  暂停查看
    function checkInfo()
    {
        // dump($this->data);die;
        if(isset($this->data['make_id'])){
            // 提貨單
            $pickupDetail = getDatas('make_pickup_detail',['*'],['make_id'=>$this->data['make_id']]);
            if(!empty($pickupDetail)){
                foreach ($pickupDetail as $k1 => $v1) {
                    $pickupDetail[$k1]['proInfo'] = getProAndProchar($v1,$this->lang,48);
                    $pickupDetail[$k1]['area_name'] = $this->saleModel->getArea_name($v1['target'],$v1['area_id']);
                    $pickupDetail[$k1]['create_name'] = getValue('admin',['id'=>$v1['create_id']],'nickname');
                    $pickupDetail[$k1]['create_time'] = $v1['create_at'] ? getTimeDates($v1['create_at'],$this->lang) : '-';
                    $pickupDetail[$k1]['cancel_name'] = getValue('admin',['id'=>$v1['cancel_id']],'nickname');
                    $pickupDetail[$k1]['cancel_time'] = $v1['cancel_at'] ? getTimeDates($v1['cancel_at'],$this->lang) : '-';
                }
            }

            // 加工單
            $processDetail = getDatas('make_process_detail',['*'],['make_id'=>$this->data['make_id']]);
            if(!empty($processDetail)){
                foreach ($processDetail as $k2 => $v2) {
                    $processDetail[$k2]['proInfo'] = getProAndProchar($v2,$this->lang,48);
                    // $processDetail[$k2]['']

                    $where = ['pro_id'=>$v2['pro_id'],'prochar_id'=>$v2['prochar_id'],'pm.lang'=>$this->lang];
                    $join = [
                                ['pro p','zu.pro_id2item = p.id'],
                                ['multi_lang pm','p.mkey = pm.mkey']
                            ];
                    $processDetail[$k2]['item'] = getSearch('pro_item','zu',['zu.*,pm.content itemName'],$where,$join,'zu.id desc');
                    if(!empty($processDetail[$k2]['item'])){
                        foreach ($processDetail[$k2]['item'] as $k => $v) {
                            $processDetail[$k2]['item'][$k]['proInfo'] = getProAndProchar(['pro_id'=>$v['pro_id2item'],'prochar_id'=>$v['prochar_id2item']],$this->lang,48);
                        }
                    }
                    $processDetail[$k2]['remark'] = getDatas('make_process_remark',['*'],['detail_id'=>$v2['id']]);
                    if($v2['process_id']){
                        $processDetail[$k2]['team_name'] = $this->saleModel->getTeam_names(getValue('make_process',['id'=>$v2['process_id']],'team_ids'));
                    }else{
                       $processDetail[$k2]['team_name'] = '-'; 
                    }

                    $processDetail[$k2]['status_name'] = '加工中';
                    $processDetail[$k2]['status_time'] = getTimeDates($v2['create_at'],$this->lang);

                    $processDetail[$k2]['status_name'] = $v2['finish_at'] ? '完成' : $processDetail[$k2]['status_name'];
                    $processDetail[$k2]['status_name'] = $v2['pause_at'] ? '暂停' : $processDetail[$k2]['status_name'];
                    $processDetail[$k2]['status_name'] = $v2['cancel_at'] ? '取消' : $processDetail[$k2]['status_name'];

                    $processDetail[$k2]['status_time'] = $v2['finish_at'] ? getTimeDates($v2['create_at'],$this->lang) : $processDetail[$k2]['status_time'];
                    $processDetail[$k2]['status_time'] = $v2['pause_at'] ? getTimeDates($v2['pause_at'],$this->lang) : $processDetail[$k2]['status_time'];
                    $processDetail[$k2]['status_time'] = $v2['cancel_at'] ? getTimeDates($v2['cancel_at'],$this->lang) : $processDetail[$k2]['status_time'];
                }
            }

            // 裝箱單
            $salePack = getDatas('sale_pack',['*'],['make_id'=>$this->data['make_id']]);
            $salePack_['headData'] = [];
            $salePack_['bodyData'] = [];
            if(!empty($salePack)){
                $salePackHeadData['pack_qty'] = 0;
                $salePackHeadData['volume'] = 0;
                $salePackHeadData['kgs'] = 0;

                foreach ($salePack as $k3 => $v3) {
                    $create_id = $v3['create_id'];
                    $sale_id = $v3['sale_id'];
                    $salePackHeadData['pack_qty'] += $v3['ctns'];
                    $salePackHeadData['volume'] += $v3['ctns']*$v3['volume'];
                    $salePackHeadData['kgs'] += $v3['ctns']*$v3['kgs'];

                    $salePack[$k3]['area_name'] = getValue('warehouse_area',['id'=>$v3['area_id']],'name');
                    $salePack[$k3]['detail'] = getDatas('sale_pack_detail',['*'],['pack_id'=>$v3['id']]);
                    if(!empty($salePack[$k3]['detail'])){
                        foreach ($salePack[$k3]['detail'] as $k_ => $v_) {
                            $salePack[$k3]['detail'][$k_]['proInfo'] = getProAndProchar($v_,$this->lang,48);
                        }
                    }
                }

                $salePackHeadData['sale_pack_name'] = getValue('admin',['id'=>$create_id],'nickname');
                $delivery_type_id = getValue('sale',['id'=>$sale_id],'delivery_type_id');
                if($delivery_type_id){
                    $salePackHeadData['delivery_type_name'] = getValue('multi_lang',['mkey'=>'delivery_type_'.$delivery_type_id,'lang'=>$this->lang],'content');
                }else{
                    $salePackHeadData['delivery_type_name'] = '-';
                }

                $salePack_['headData'] = $salePackHeadData;
                $salePack_['bodyData'] = $salePack;
            }


            // 获取物流信息
            $where_['id'] = $this->data['make_id'];
            $sale = Db::name('sale')
                        ->where('id', 'IN', function ($query) use ($where_) {
                            $query->name('sale_make')->where($where_)->field('sale_id');
                        })
                        ->find();
            $sale['delivery_type_name'] = getValue('multi_lang',['mkey'=>'sale_delivery_type_'.$sale['delivery_type_id'],'lang'=>$this->lang],'content');
            if($sale['delivery_type_id'] != 4){
                $make_delivery_detail_where = ['make_id'=>$this->data['make_id']];
                $make_delivery = Db::name('make_delivery')
                                ->where('id', 'IN', function ($query) use ($make_delivery_detail_where) {
                                    $query->name('make_delivery_detail')->where($make_delivery_detail_where)->field('delivery_id');
                                })
                                ->find();
                $sale['create_name'] = getValue('admin',['id'=>$make_delivery['create_id']],'nickname');
                $sale['create_time'] = getTimeDates($make_delivery['create_at'],$this->lang);

                // 公司和分公司名称
                $sale['logisticsCompany_name'] = '-';
                $sale['logisticsBranch_name'] = '-';

                // 回执
                $make_delivery_detail = Db::name('make_delivery_detail')->where(['delivery_id'=>$make_delivery['id'],'receipt_id'=>['gt',0]])->find();
                $sale['receipt_name'] = $make_delivery_detail ? getValue('admin',['id'=>$make_delivery_detail['receipt_id']],'nickname') : '-'; 
                $sale['receipt_time'] = $make_delivery_detail ? getTimeDates($make_delivery_detail['receive_at'],$this->lang) : "-";

                // 组员
                $teamsWhere = ['delivery_id'=>$make_delivery['id']];
                $sale['teams'] = Db::name('admin')
                                ->where('id', 'IN', function ($query) use ($teamsWhere) {
                                    $query->name('make_delivery_team')->where($teamsWhere)->field('admin_id');
                                })
                                ->select();

                $sale['work_time'] = $make_delivery['finish_at'] ? date("H:i:s",$make_delivery['finish_at'] - $make_delivery['start_at']) : '送货中';

            }else{
                $logistics = Db::name('make_logistics')->where(['make_id'=>$this->data['make_id']])->find();
                $sale['create_name'] = getValue('admin',['id'=>$logistics['create_id']],'nickname');
                $sale['create_time'] = getTimeDates($logistics['create_at'],$this->lang);

                // 公司和分公司名称
                $sale['logisticsCompany_name'] = getValue('company',['id'=>$sale['trans_id']],'name');

                // 回执
                $sale['receipt_name'] = '-'; 
                $sale['receipt_time'] = '-';

                // 组员
                $teamsWhere = ['logistics_id'=>$logistics['id']];
                $sale['teams'] = Db::name('admin')
                                ->where('id', 'IN', function ($query) use ($teamsWhere) {
                                    $query->name('make_logistics_team')->where($teamsWhere)->field('admin_id');
                                })
                                ->select();

                $sale['work_time'] = $logistics['receive_at'] ? date("H:i:s",$logistics['receive_at'] - $logistics['start_at']) : '送货中';

                // 回执
                $sale['receipt_name'] = '-'; 
                $sale['receipt_time'] = "-";
            }
            // dump($sale);die;







            $returnData['logisticsDetail'] = $sale;
            $returnData['pickupDetail'] = $pickupDetail;
            $returnData['processDetail'] = $processDetail;
            $returnData['salePacksalePack'] = $salePack_;
            return json_encode($returnData);
        }
    }

    // 暂停 -- 取消暂停
    function cancelPause()
    {
        return $this->pauseAction($this->data['sale_id'],false);
    }

    // 暂停 -- 解锁
    function unLock()
    {
        if($this->data['sale_id']){
            $res = updateData('sale',['id'=>$this->data['sale_id']],['lock_at'=>0]);
            if($res >= 0 ){
                return json_encode(array('status'=>2,'msg'=>'通知解锁 成功'));
            }else{
                return json_encode(array('status'=>2,'msg'=>'通知解锁 失败'));
            }
        }else{
            return json_encode(array('status'=>2,'msg'=>'通知解锁 失败'));
        }
    }

    // 获取配货id
    function getSaleMakeId($sale_id,$warehouse_id)
    {
        return getValue('sale_make',['sale_id'=>$sale_id,'warehouse_id'=>$warehouse_id],'id');
    }


    // 取消--回复
    function cancelRestore()
    {
        if(isset($this->data['sale_id'])){
            return $this->sale_makeCancel($this->data['sale_id'],false);
        }else{
            return json_encode(array('status'=>2,'msg'=>'通知配货恢复失败'));
        }

    }
    function sale_makeCancel($sale_id,$type = true)
    {
        if($type){
            $updateData = ['cancel_id'=>$this->user_id,'cancel_at'=>time()];
            $msg = '通知配货取消成功';
        }else{
            $updateData = ['cancel_id'=>0,'cancel_at'=>0];
            $msg = '通知配货恢复成功';
        }
        updateData('sale_make',['sale_id'=>$sale_id],$updateData);
        // 编辑提货取消
        updateData('make_pickup_detail',['sale_id'=>$sale_id],$updateData);
        $pickip_id = implode( array_unique( array_column( getDatas('make_pickup_detail',['pickup_id'],['sale_id'=>$sale_id,'pickup_id'=>['gt',0]]),'pickup_id' ) ),',');
        if($pickip_id) updateData('make_pickup',['id'=>['in',$pickip_id]],$updateData);
        // 编辑加工取消
        updateData('make_pickup_detail',['sale_id'=>$sale_id],$updateData);
        $process_id = implode( array_unique( array_column( getDatas('make_process_detail',['process_id'],['sale_id'=>$sale_id,'process_id'=>['gt',0]]),'process_id' ) ),',');
        if($process_id) updateData('make_process',['id'=>['in',$process_id]],$updateData);

        $this->saleModel->addSaleLog($sale_id,'makeMake',$msg);
        return json_encode(array('status'=>1,'msg'=>$msg));

    }

    // 缺货登记 -- 查看
    function missingCheck()
    {
        return json_encode($this->getStoreMissing());
    }

    // 取消缺货等级
    function missingCancel()
    {
        if(isset($this->data['missing_id'])){
            updateData('store_missing',['id'=>$this->data['missing_id']],['del_id'=>$this->user_id,'del_at'=>time()]);
            // deleteData('store_missing',['id'=>$this->data['missing_id']]);
            // deleteData('store_missing_detail',['missing_id'=>$this->data['missing_id']]);
            return json_encode(array('status'=>1,'msg'=>'取消成功'));
        }else{
            return json_encode(array('status'=>2,'msg'=>'选择正确的数据'));
        }
    }

    function print()
    {
        return 1;
    }

}
